package com.lei.demo.service.imp;

import com.lei.demo.dao.UserDAO;
import com.lei.demo.entity.User;
import com.lei.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * Created by Shinelon on 2017/6/8.
 */
@Service("UserService")
@Transactional(readOnly = true)
public class UserServiceImpl implements UserService {

    @Autowired
    UserDAO userDAO;

    @Override
    public User findById(int id) {
        return userDAO.findById(id);
    }

    @Transactional(readOnly = false)
    @Override
    public void deleteUser(User user) {
        userDAO.delete(user);
    }

    @Transactional(readOnly = false)
    @Override
    public void saveUser(User user) {
        userDAO.save(user);
    }

    @Transactional(readOnly = false)
    @Override
    public void updateUser(User user) {
        userDAO.update(user);
    }

    @Override
    public Boolean login(int id, String pwd) {
        User user = userDAO.findById(id);
        if(user == null || pwd == null) {
            return false;
        } else if(user.getPwd() != pwd ){
            return false;
        } else
            return true;
    }

    @Override
    public User findByName(String name) {
        List<User> users = findUsers(name);
        if(!users.isEmpty()) {
            return users.get(0);
        } else {
            return null;
        }
    }

    @Override
    @Transactional(readOnly = false)
    public void deleteUser(String name) {
        List<User> users=findUsers(name);
        for (User user : users) {
            deleteUser(user);
        }
    }

    @Override
    public List<User> findUsers(String name) {
        return userDAO.findUsers(name);
    }

    @Override
    public List<User> findAllUser() {
        return null;
    }
}
